package com.archtecture.model.patterns;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.entities.utils.ModelAb;
import com.spgf.model.utils.AndroidUtil;

public abstract class PadraoFragmentMnt<Model extends ModelAb> extends PadraoFragment<Model> implements OnItemLongClickListener {

	protected ListView	listview;

	public void atualizarLista() throws NegocioException {}

	public void configurarExcluir(Integer pCodigo) {

		try {
			executarExcluir(pCodigo);
			atualizarLista();
			Toast.makeText(rootView.getContext(), "Registro excluido com sucesso!", Toast.LENGTH_LONG).show();
		} catch (NegocioException e) {
			Toast.makeText(rootView.getContext(), AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

	public abstract void configurarTela(LayoutInflater inflater, ViewGroup container) throws NegocioException;

	protected abstract void executarExcluir(Integer pCodigo) throws NegocioException;

	protected abstract boolean executarLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException;

	public abstract Class<?> getControleCadastro();

	protected abstract List<Model> getListaModel() throws NegocioException;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		try {
			configurarTela(inflater, container);
		} catch (NegocioException e) {
			Toast.makeText(rootView.getContext(), AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}

		return rootView;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> pParent, View pView, int pPosition, long pID) {
		try {
			return executarLongClick(pParent, pView, pPosition, pID);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
			return false;
		}
	}
	
	@Override
	public void onResume() {
		try {
			atualizarLista();
		} catch (NegocioException e) {
			Toast.makeText(this.getActivity(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
		}
		super.onResume();
	}

}
